home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HPAVC
/
HPAVC CD-ROM.iso
/
WD_SRC.ZIP
/
SYSTEM
/
DYNARRAY.CPP
next >
Wrap
C/C++ Source or Header
|
1994-12-19
|
14KB
|
700 lines
#include "..\Source\LastWolf.hpp"
// Functions for dynamic array classes
// Goes: DWORD functions, WORD functions, BYTE functions, and Pointer functions
// DWORD functions
//-------------------------------------------------
MDWordArray::MDWordArray()
{
pDWordArray = NULL;
nElements = 0;
}
MDWordArray::~MDWordArray()
{
if( nElements > 0 )
free( pDWordArray );
}
MDWordArray &MDWordArray::operator=( MDWordArray ©From )
{
SetSize( copyFrom.nElements );
memcpy( pDWordArray, copyFrom.pDWordArray, sizeof(DWORD)*nElements );
return *this;
}
DWORD MDWordArray::Get( UWORD index )
{
#ifdef CHECK_DYNARRAYS
CheckIndex(index);
#endif
return pDWordArray[index];
}
BOOL MDWordArray::Set( UWORD index, DWORD newVal )
{
#ifdef CHECK_DYNARRAYS
CheckIndex(index);
#endif
pDWordArray[index] = newVal;
return TRUE;
}
UWORD MDWordArray::NumElements()
{
return nElements;
}
BOOL MDWordArray::Insert( UWORD index, DWORD toInsert )
{
DWORD *newArray;
#ifdef CHECK_DYNARRAYS
++nElements;
CheckIndex(index);
--nElements;
#endif
// Create a new array.
newArray = new DWORD[nElements+1];
// Copy the old array into the new one, start inserting at index.
memcpy( newArray, pDWordArray, index*sizeof(DWORD) );
memcpy( &newArray[index+1], &pDWordArray[index], (nElements-index)*sizeof(DWORD) );
++nElements;
// Free the old array and set our pointer to the new one
free( pDWordArray );
pDWordArray = newArray;
// Put the new value in there
Set( index, toInsert );
return TRUE;
}
BOOL MDWordArray::Delete( UWORD index )
{
DWORD *newArray;
UWORD copyIndex;
BOOL bDeleted;
#ifdef CHECK_DYNARRAYS
CheckIndex(index);
#endif
// Special case where we don't need to realloc a new array and copy...
if( --nElements == 0 )
{
free( pDWordArray );
pDWordArray = NULL;
return TRUE;
}
// Create a new array
newArray = new DWORD[nElements];
// Copy the old array into the new one, skipping the one at index
bDeleted = FALSE;
for( copyIndex=0; copyIndex < NumElements(); copyIndex++ )
{
// If we're at index, skip the one we want to delete
if( copyIndex == index )
bDeleted = TRUE;
if( bDeleted == TRUE )
newArray[copyIndex] = pDWordArray[copyIndex+1];
else
newArray[copyIndex] = pDWordArray[copyIndex];
}
// Free the old array and set our pointer to the new one
free( pDWordArray );
pDWordArray = newArray;
return TRUE;
}
BOOL MDWordArray::Append( DWORD toAdd )
{
// Just insert an element at the end
return Insert( nElements, toAdd );
}
BOOL MDWordArray::SetSize( UWORD newSize )
{
free( pDWordArray );
pDWordArray = NULL;
nElements = newSize;
if( newSize > 0 )
pDWordArray = new DWORD[newSize];
return TRUE;
}
BOOL MDWordArray::CheckIndex( UWORD index )
{
char error[256];
if( index < nElements )
return TRUE;
sprintf( error, "Invalid index, %d, attempted in MDWordArray, exiting.", index );
puts( error );
return FALSE;
}
// WORD functions
//-------------------------------------------------
MWordArray::MWordArray()
{
pWordArray = NULL;
nElements = 0;
}
MWordArray::~MWordArray()
{
if( nElements > 0 )
free( pWordArray );
}
MWordArray &MWordArray::operator=( MWordArray ©From )
{
SetSize( copyFrom.nElements );
memcpy( pWordArray, copyFrom.pWordArray, sizeof(WORD)*nElements );
return *this;
}
WORD MWordArray::Get( UWORD index )
{
#ifdef CHECK_DYNARRAYS
CheckIndex(index);
#endif
return pWordArray[index];
}
BOOL MWordArray::Set( UWORD index, WORD newVal )
{
#ifdef CHECK_DYNARRAYS
CheckIndex(index);
#endif
pWordArray[index] = newVal;
return TRUE;
}
UWORD MWordArray::NumElements()
{
return nElements;
}
BOOL MWordArray::Insert( UWORD index, WORD toInsert )
{
WORD *newArray;
#ifdef CHECK_DYNARRAYS
++nElements;
CheckIndex(index);
--nElements;
#endif
// Create a new array
newArray = new WORD[nElements+1];
// Copy the old array into the new one, start inserting at index.
memcpy( newArray, pWordArray, index*sizeof(DWORD) );
memcpy( &newArray[index+1], &pWordArray[index], (nElements-index)*sizeof(DWORD) );
++nElements;
// Free the old array and set our pointer to the new one
free( pWordArray );
pWordArray = newArray;
// Put the new value in there
Set( index, toInsert );
return TRUE;
}
BOOL MWordArray::Delete( UWORD index )
{
WORD *newArray;
UWORD copyIndex;
BOOL bDeleted;
#ifdef CHECK_DYNARRAYS
CheckIndex(index);
#endif
// Special case where we don't need to realloc a new array and copy...
if( --nElements == 0 )
{
free( pWordArray );
pWordArray = NULL;
return TRUE;
}
// Create a new array
newArray = new WORD[nElements];
// Copy the old array into the new one, skipping the one at index
bDeleted = FALSE;
for( copyIndex=0; copyIndex < NumElements(); copyIndex++ )
{
// If we're at index, skip the one we want to delete
if( copyIndex == index )
bDeleted = TRUE;
if( bDeleted == TRUE )
newArray[copyIndex] = pWordArray[copyIndex+1];
else
newArray[copyIndex] = pWordArray[copyIndex];
}
// Free the old array and set our pointer to the new one
free( pWordArray );
pWordArray = newArray;
return TRUE;
}
BOOL MWordArray::Append( WORD toAdd )
{
// Just insert an element at the end
return Insert( nElements, toAdd );
}
BOOL MWordArray::SetSize( UWORD newSize )
{
free( pWordArray );
pWordArray = NULL;
nElements = newSize;
if( newSize > 0 )
pWordArray = new WORD[newSize];
return TRUE;
}
BOOL MWordArray::CheckIndex( UWORD index )
{
char error[256];
if( index < nElements )
return TRUE;
sprintf( error, "Invalid index, %d, attempted in MWordArray, exiting.", index );
puts( error );
return FALSE;
}
// BYTE functions
//-------------------------------------------------
MByteArray::MByteArray()
{
pByteArray = NULL;
nElements = 0;
}
MByteArray::~MByteArray()
{
if( nElements > 0 )
free( pByteArray );
}
MByteArray &MByteArray::operator=( MByteArray ©From )
{
SetSize( copyFrom.nElements );
memcpy( pByteArray, copyFrom.pByteArray, sizeof(BYTE)*nElements );
return *this;
}
BYTE MByteArray::Get( UWORD index )
{
#ifdef CHECK_DYNARRAYS
CheckIndex(index);
#endif
return pByteArray[index];
}
BOOL MByteArray::Set( UWORD index, BYTE newVal )
{
#ifdef CHECK_DYNARRAYS
CheckIndex(index);
#endif
pByteArray[index] = newVal;
return TRUE;
}
UWORD MByteArray::NumElements()
{
return nElements;
}
BOOL MByteArray::Insert( UWORD index, BYTE toInsert )
{
BYTE *newArray;
#ifdef CHECK_DYNARRAYS
++nElements;
CheckIndex(index);
--nElements;
#endif
// Create a new array
newArray = new BYTE[nElements+1];
// Copy the old array into the new one, start inserting at index.
memcpy( newArray, pByteArray, index*sizeof(DWORD) );
memcpy( &newArray[index+1], &pByteArray[index], (nElements-index)*sizeof(DWORD) );
++nElements;
// Free the old array and set our pointer to the new one
free( pByteArray );
pByteArray = newArray;
// Put the new value in there
Set( index, toInsert );
return TRUE;
}
BOOL MByteArray::Delete( UWORD index )
{
BYTE *newArray;
UWORD copyIndex;
BOOL bDeleted;
#ifdef CHECK_DYNARRAYS
CheckIndex(index);
#endif
// Special case where we don't need to realloc a new array and copy...
if( --nElements == 0 )
{
free( pByteArray );
pByteArray = NULL;
return TRUE;
}
// Create a new array
newArray = new BYTE[nElements];
// Copy the old array into the new one, skipping the one at index
bDeleted = FALSE;
for( copyIndex=0; copyIndex < NumElements(); copyIndex++ )
{
// If we're at index, skip the one we want to delete
if( copyIndex == index )
bDeleted = TRUE;
if( bDeleted == TRUE )
newArray[copyIndex] = pByteArray[copyIndex+1];
else
newArray[copyIndex] = pByteArray[copyIndex];
}
// Free the old array and set our pointer to the new one
free( pByteArray );
pByteArray = newArray;
return TRUE;
}
BOOL MByteArray::Append( BYTE toAdd )
{
// Just insert an element at the end
return Insert( nElements, toAdd );
}
BOOL MByteArray::SetSize( UWORD newSize )
{
free( pByteArray );
pByteArray = NULL;
nElements = newSize;
if( newSize > 0 )
pByteArray = new BYTE[newSize];
return TRUE;
}
BOOL MByteArray::CheckIndex( UWORD index )
{
char error[256];
if( index < nElements )
return TRUE;
sprintf( error, "Invalid index, %d, attempted in MByteArray, exiting.", index );
puts( error );
return FALSE;
}
// Pointer functions
//-------------------------------------------------
MPointerArray::MPointerArray()
{
pPointerArray = NULL;
nElements = 0;
}
MPointerArray::~MPointerArray()
{
if( nElements > 0 )
free( pPointerArray );
}
MPointerArray &MPointerArray::operator=( MPointerArray ©From )
{
SetSize( copyFrom.nElements );
memcpy( pPointerArray, copyFrom.pPointerArray, sizeof(void *)*nElements );
return *this;
}
void *MPointerArray::Get( UWORD index )
{
#ifdef CHECK_DYNARRAYS
CheckIndex(index);
#endif
return pPointerArray[index];
}
BOOL MPointerArray::Set( UWORD index, void *newVal )
{
#ifdef CHECK_DYNARRAYS
CheckIndex(index);
#endif
pPointerArray[index] = newVal;
return TRUE;
}
UWORD MPointerArray::NumElements()
{
return nElements;
}
BOOL MPointerArray::Insert( UWORD index, void *toInsert )
{
void **newArray;
#ifdef CHECK_DYNARRAYS
++nElements;
CheckIndex(index);
--nElements;
#endif
// Create a new array
newArray = new void *[nElements+1];
// Copy the old array into the new one, start inserting at index.
memcpy( newArray, pPointerArray, index*sizeof(DWORD) );
memcpy( &newArray[index+1], &pPointerArray[index], (nElements-index)*sizeof(DWORD) );
++nElements;
// Free the old array and set our pointer to the new one
free( pPointerArray );
pPointerArray = newArray;
// Put the new value in there
Set( index, toInsert );
return TRUE;
}
BOOL MPointerArray::Delete( UWORD index )
{
void **newArray;
UWORD copyIndex;
BOOL bDeleted;
#ifdef CHECK_DYNARRAYS
CheckIndex(index);
#endif
// Special case where we don't need to realloc a new array and copy...
if( --nElements == 0 )
{
free( pPointerArray );
pPointerArray = NULL;
return TRUE;
}
// Create a new array
newArray = new void *[nElements];
// Copy the old array into the new one, skipping the one at index
bDeleted = FALSE;
for( copyIndex=0; copyIndex < NumElements(); copyIndex++ )
{
// If we're at index, skip the one we want to delete
if( copyIndex == index )
bDeleted = TRUE;
if( bDeleted == TRUE )
newArray[copyIndex] = pPointerArray[copyIndex+1];
else
newArray[copyIndex] = pPointerArray[copyIndex];
}
// Free the old array and set our pointer to the new one
free( pPointerArray );
pPointerArray = newArray;
return TRUE;
}
BOOL MPointerArray::Append( void *toAdd )
{
// Just insert an element at the end
return Insert( nElements, toAdd );
}
BOOL MPointerArray::SetSize( UWORD newSize )
{
free( pPointerArray );
pPointerArray = NULL;
nElements = newSize;
if( newSize > 0 )
pPointerArray = new void *[newSize];
return TRUE;
}
Index MPointerArray::GetIndexFromPointer( void *pMatch )
{
WORD retVal;
retVal = ScanForPointer( pMatch, pPointerArray, nElements );
if( retVal != -1 )
return (Index)(nElements - retVal - 1);
else
return BAD_INDEX;
/*
for( i=0; i < nElements; i++ )
if( pPointerArray[i] == pMatch )
return i;
*/
}
BOOL MPointerArray::CheckIndex( UWORD index )
{
char error[256];
if( index < nElements )
return TRUE;
sprintf( error, "Invalid index, %d, attempted in MPointerArray, exiting.", index );
puts( error );
return FALSE;
}
#pragma aux ScanForPointer = \
"repnz scasd" \
"sub edi, 4" \
"cmp eax,[edi]" \
"je FOUND" \
"mov cx,-1" \
"FOUND:" \
value [cx] \
parm [eax] [edi] [ecx];